from Parser import Parser
import numpy as np

class Semantic(Parser):
    def init(self):
        self.result = {
            'dynamic': 0,
            'grid': 0,
            'title': "",
            'xLabel': "",
            'yLabel': "",
            'lines': []
        }

    # 计算xy
    def getDots(self, x, y):
        x *= self.x_scale
        y *= self.y_scale
        temp1 = x * np.cos(self.rot) + y * np.sin(self.rot)
        y *= np.cos(self.rot) - x * np.sin(self.rot)
        x = temp1
        x += self.x_origin
        y += self.y_origin
        dots = [ {'x': x[i], 'y':y[i]} for i in range(len(x))]
        return dots

    def getScatterDots(self, x, y, r):
        x *= self.x_scale
        y *= self.y_scale
        temp1 = x * np.cos(self.rot) + y * np.sin(self.rot)
        y *= np.cos(self.rot) - x * np.sin(self.rot)
        x = temp1
        x += self.x_origin
        y += self.y_origin
        if not hasattr(x, '__iter__'):
            x = [x]
        if not hasattr(y, '__iter__'):
            y = [y]
        if not hasattr(r, '__iter__'):
            r = [r]
        dots = [ {'x': x[i], 'y':y[i], 'r':r[i]} for i in range(len(x))]
        return dots

    # override
    def recordLine(self):
        dots = None
        if self.lineType == "scatter":
            dots = self.getScatterDots(self.x_ptr, self.y_ptr, self.radius)
        else:
            dots = self.getDots(self.x_ptr, self.y_ptr)
        line = {
            'color': self.color,
            'lineType': self.lineType,
            'dots': dots,
            'width': self.lineWidth
        }
        self.result['lines'].append(line)

    # get final result
    # {
    #     "dynamic": 0,
    #     "grid": 0,
    #     "title": "",
    #     "xLabel": "x轴名称",
    #     "yLabel": "y轴名称",
    #     "lines": [
    #         {
    #             "color": "red",
    #             "lineType": "dotted",
    #             "dots": [
    #                 {
    #                     "x": 1.0,
    #                     "y": 2.0
    #                 },{
    #                     "x": 2.0,
    #                     "y": 4.0
    #                 },{
    #                     "x": 3.0,
    #                     "y": 6.0
    #                 }
    #             ]
    #         },
    #         {
    #             "color": "blue",
    #             "lineType": "dotted",
    #             "dots": [
    #                 {
    #                     "x": 1.0,
    #                     "y": 2.0
    #                 },{
    #                     "x": 2.0,
    #                     "y": 4.0
    #                 },{
    #                     "x": 3.0,
    #                     "y": 6.0
    #                 }
    #             ]
    #         }
    #     ]
    # }
    def getResult(self):
        self.result['dynamic'] = self.dynamicOn
        self.result['grid'] = self.gridOn
        self.result['title'] = self.title
        self.result['xLabel'] = self.x_label
        self.result['yLabel'] = self.y_label
        return self.result